<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
      
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="上级分类">
          <a-tree-select
            style="width:100%"
            :dropdownStyle="{ maxHeight: '200px', overflow: 'auto' }"
            :treeData="treeData"
            v-model="model.parentId"
            placeholder="请选择上级分类"
            >
          </a-tree-select>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="分类名称">
          <a-input placeholder="请输入分类名称" v-decorator="['name', validatorRules.name ]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="分类关键字">
          <a-input placeholder="请输入分类关键字" v-decorator="['keywords', validatorRules.keywords ]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="分类介绍">
          <a-input placeholder="请输入分类介绍" v-decorator="['description', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="分类图标">
          <a-upload
            name="file"
            listType="picture-card"
            accept="image/*"
            class="avatar-uploader"
            :showUploadList="false"
            :action="doMain + 'sys/common/upload'"
            :beforeUpload="beforeUpload"
            @change="handleChange"
          >
            <img v-if="model.iconUrl" :src="model.iconUrl" alt="avatar" style="height:110px;"/>
            <div v-else>
                <a-icon :type="loading ? 'loading' : 'plus'" />
                <div class="ant-upload-text">选择图片</div>
            </div>
          </a-upload>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="分类图片">
          <a-input placeholder="请输入分类图片" v-decorator="['picUrl', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="排序">
          <a-input placeholder="请输入排序" v-decorator="['sortNo', {}]" />
        </a-form-item>		
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import moment from "moment"
  import { getGoodsCategoryTreeList, doMain, imgView } from '@/api/api'

  export default {
    name: "CategoryModal",
    data () {
      return {
        title:"操作",
        visible: false,
        doMain: doMain,
        imgView: imgView,
        treeData:[],
        model: {},
        loading: false,
        imageUrl: '',
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules:{
        name:{rules: [{ required: true, message: '请输入分类名称!' }]},
        keywords:{rules: [{ required: true, message: '请输入分类关键字' }]},
        },
        url: {
          add: "/goods/category/add",
          edit: "/goods/category/edit",
        },
      }
    },
    created () {
    },
    methods: {
      loadTree(){
        var that = this;
        getGoodsCategoryTreeList().then((res)=>{
          if(res.success){
            console.log('----getGoodsCategoryList---')
            that.treeData = [];
            let treeList = res.result.treeList
            for(let a=0;a<treeList.length;a++){
              let temp = treeList[a];
              temp.isLeaf = temp.leaf;
              that.treeData.push(temp);
            }
          }
        });
      },
      add () {
        this.edit({'sortNo':1});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.loadTree();
          this.form.setFieldsValue(pick(this.model,'name','keywords','description','parentId','iconUrl','picUrl','level','sortNo','delFlag'))
		  //时间格式化
        });

      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            //时间格式化
            
            console.log(formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })



          }
        })
      },
      handleCancel () {
        this.close()
      },
      handleChange (info) {
        if (info.file.status === 'uploading') {
          this.loading = true
          return
        }
        if (info.file.status === 'done') {
          // Get this url from response in real world.
          this.model.iconUrl = imgView + info.file.response.message
          this.loading = false
        }
      },
      beforeUpload (file) {
        const isLt2M = file.size / 1024 / 1024 < 2
        if (!isLt2M) {
          this.$message.error('图片超过 2MB!')
        }
        return isLt2M
      },

    }
  }
</script>

<style lang="less" scoped>

</style>